package com.dragon.modules.yuegou.service.impl.system;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.system.UserRoleRefEntity;
import com.dragon.modules.yuegou.enums.code.SystemCode;
import com.dragon.modules.yuegou.mapper.system.UserRoleRefMapper;
import com.dragon.modules.yuegou.model.system.UserRoleRef;
import com.dragon.modules.yuegou.query.system.UserRoleRefQuery;
import com.dragon.modules.yuegou.service.system.UserRoleRefService;
import com.dragon.modules.yuegou.web.Check;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户角色对应关系表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-03-15 15:37:59
 * @version 2.0
 */
@Service("sysUserRoleRefService")
public class UserRoleRefServiceImpl
		extends ServiceImpl<UserRoleRefMapper, UserRoleRefEntity>
		implements UserRoleRefService {

	@Transactional
	@Override
	public void authorization(List<UserRoleRef> userRoleRefList) {
		UserRoleRefQuery userRoleRefQuery = new UserRoleRefQuery();
		if (userRoleRefList.size()>0) {
			//删除该类角色的所有记录
			userRoleRefQuery.setRoleId(userRoleRefList.get(0).getRoleId());
			baseMapper.delete(userRoleRefQuery.toQueryWrapper());
			
			for (UserRoleRef userRoleRef : userRoleRefList) {
				//删除该用户的角色记录
				userRoleRefQuery.setRoleId(null);
				userRoleRefQuery.setUserId(userRoleRef.getUserId());
				baseMapper.delete(userRoleRefQuery.toQueryWrapper());
				//插入新的角色授权
				save(userRoleRef);
			}
		}
	}

	@Override
	public int countGroupAdminUser(String groupId) {
		Check.notEmpty(groupId, SystemCode.GROUP_ID_NOTNULL.getMessage());
		return baseMapper.countGroupAdminUser(groupId);
	}

	@Override
	public int countPlatformAdminUser() {
		return baseMapper.countPlatformAdminUser();
	}

}